package main;

import control.bean.ControlPropertiesBean;
import data.FormTemplateManager;
import ui.FormArea;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/8/13
 * Time: 5:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class FormBuilder extends JFrame{

    /*
     * TODO checklist here
     * - drag and drop cell
     * - make properties hide when not in focus
     * - resize cell, rows when required by the controls
     * - main toolbar functions
     * - add a save and load button
     * - add a remove control funciton
     * - remove row function / auto or manual would work
     * - finish all the beans export and import
     */
    FormBuilder() {
        super("FromBuilder I2");
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        //setGlassPane(new GhostGlassPane());
        setMinimumSize(new Dimension(860, 700));

        initComponent();
        getContentPane().setBackground(Color.WHITE);
        Toolkit.getDefaultToolkit().setDynamicLayout(true);
        pack();
        setLocationRelativeTo(null);
    }

    void initComponent() {
        add(mainToolbar(), BorderLayout.NORTH);
        add(formPanel(), BorderLayout.CENTER);
        add(buttons(), BorderLayout.SOUTH);
    }

    Component mainToolbar() {
        JPanel mainToolbar = new JPanel(new BorderLayout());
        mainToolbar.setOpaque(false);

        JComboBox fonts               = new JComboBox();
        JComboBox fontSize            = new JComboBox();
        JComboBox color               = new JComboBox();
        JComboBox labelPositionButton = new JComboBox();
        JButton boldButton          = new JButton("B");
        JButton italicsButton       = new JButton("I");
        JButton underlineButton     = new JButton("U");
        JButton propertiesButton    = new JButton("Properties");

        fonts.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Consolas", "Courier New", "Dialog Input", "Monospaced" }));

        fontSize.setModel(new DefaultComboBoxModel(new String[] {"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}));

        labelPositionButton.setModel(new DefaultComboBoxModel(new String[] {"Align Left", "Align Center", "Align Right"}));

        JPanel frontPortion = new JPanel(new FlowLayout(FlowLayout.LEADING));
        frontPortion.setOpaque(false);
        frontPortion.add(fonts);
        frontPortion.add(fontSize);

        JPanel buttonStyle = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
        buttonStyle.setOpaque(false);
        buttonStyle.add(boldButton);
        buttonStyle.add(italicsButton);
        buttonStyle.add(underlineButton);

        frontPortion.add(buttonStyle);
        frontPortion.add(labelPositionButton);

        JPanel backPortion = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        backPortion.setOpaque(false);
        backPortion.add(propertiesButton);

        mainToolbar.add(frontPortion, BorderLayout.CENTER);
        mainToolbar.add(backPortion, BorderLayout.EAST);

//        GroupLayout layout = new GroupLayout(mainToolbar);

//        propertiesButton.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseReleased(MouseEvent e) {
//                FormTemplateManager manager = new FormTemplateManager();
//
//                boolean testingSave = false;
//
//                manager.setFormTemplatePath(new File("C:/Users/Deshun/Documents/NetBeansProjects/FormBuilder0.1/abc.xml"));
//                if ( testingSave ) {
//                } else {
//                    System.out.println("Loading");
//                    ArrayList<ControlPropertiesBean> cpbTemplateLoaded = manager.loadTemplate("");
//                    formArea.importForm(cpbTemplateLoaded);
//                    for (ControlPropertiesBean cpbL : cpbTemplateLoaded){
//                        System.out.println(cpbL);
//                    }
//                }
//            }
//        });

        /*mainToolbar.setLayout(layout);

        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(fonts, 170, 170, 170)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                .addComponent(fontSize, 55, 55, 55)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                .addComponent(boldButton, 35, 35, 35)
                                .addComponent(italicsButton, 35, 35, 35)
                                .addComponent(underlineButton, 35, 35, 35)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                .addComponent(color, 55, 55, 55)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                .addComponent(labelPositionButton, 150, 150, 150)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(propertiesButton, 100, 100, 100)
                                .addContainerGap()
                        )
        );

        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap(3, 3)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addComponent(fonts, 35, 35, 35)
                                        .addComponent(fontSize, 35, 35, 35)
                                        .addComponent(boldButton, 35, 35, 35)
                                        .addComponent(italicsButton, 35, 35, 35)
                                        .addComponent(underlineButton, 35, 35, 35)
                                        .addComponent(color, 35, 35, 35)
                                        .addComponent(labelPositionButton, 35, 35, 35)
                                        .addComponent(propertiesButton, 35, 35, 35))
                        )
        );*/

        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);
        panel.add(mainToolbar, BorderLayout.CENTER);
        panel.add(new JSeparator(), BorderLayout.SOUTH);
        return panel;
    }

    FormArea formArea;
    Component formPanel() {
        formArea = new FormArea();
        return formArea.buildFormArea();
    }

    Component buttons() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonPanel.setOpaque(false);

        JButton save = new JButton("Save");
        JButton load = new JButton("Load");


        save.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("Exporting");
                FormTemplateManager manager = new FormTemplateManager();
                manager.setFormTemplatePath(new File("C:/Users/Deshun/Documents/NetBeansProjects/FormBuilder0.1/abc.xml"));
                manager.saveTemplate("", ( ArrayList<ControlPropertiesBean> ) formArea.exportForm());
            }
        });

        load.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("Loading");
                FormTemplateManager manager = new FormTemplateManager();
                manager.setFormTemplatePath(new File("C:/Users/Deshun/Documents/NetBeansProjects/FormBuilder0.1/abc.xml"));
                formArea.importForm(manager.loadTemplate(""));
            }
        });

        buttonPanel.add(save);
        buttonPanel.add(load);

        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setOpaque(false);
        wrapper.add(new JSeparator(), BorderLayout.NORTH);
        wrapper.add(buttonPanel, BorderLayout.CENTER);

        return wrapper;
    }

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (UnsupportedLookAndFeelException e) {
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                FormBuilder frame = new FormBuilder();
                frame.setVisible(true);
            }
        });
    }

}
